home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / video / vidmap / glxhelper.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  166 lines

  1. /*
  2.  * glxhelper.c:
  3.  *
  4.  *   This file provides a helper function "GLXCreateWindow", which does
  5.  * all the necessary magic to create an X window suitable for GL drawing 
  6.  * to take place within.   see the definition of GLXCreateWindow for a 
  7.  * description of how to call it.
  8.  */
  9.  
  10. #include    <X11/Xlib.h>
  11. #include    <X11/Xutil.h>
  12. #include    <gl/glws.h>  
  13. #include    "glxhelper.h"
  14.  
  15. char *typeToName[] = {
  16.     "color index single buffer",
  17.     "color index double buffer",
  18.     "rgb single buffer",
  19.     "rgb double buffer",
  20. };
  21.  
  22. /*
  23.  * Dorky little helper function used to build up a GLXconfig array.
  24.  */
  25.  
  26. static void set_entry (GLXconfig* ptr, int b, int m, int a)
  27. {
  28.     ptr->buffer = b;
  29.     ptr->mode = m;
  30.     ptr->arg = a;
  31. }
  32.  
  33. /*
  34.  * GLXCreateWindow(dpy, parent, x, y, w, h, boderWidth, type)
  35.  *
  36.  * Return value is the X window id of the newly created window.
  37.  *
  38.  * Arguments are:
  39.  *    dpy        The X "Display*" returned by XOpenDisplay
  40.  *    parent        The parent of the newly created window,
  41.  *            a typical value for this is
  42.  *            RootWindow(dpy, DefaultScreen(dpy))
  43.  *    x,y        The location of the window to be created,
  44.  *            y coordinate is measured from the top down.
  45.  *    w,h        size of the new window
  46.  *    borderWidth    the X border size for this window, should probably
  47.  *            be zero.
  48.  *    type        the GLXWindowType (see glxhelper.h) desribing the
  49.  *            typer of GL drawing to be done in this window
  50.  */
  51. Window GLXCreateWindow(Display* dpy, Window parent, int x, int y, int w, int h,
  52.                int borderWidth, GLXWindowType type)
  53. {
  54.     GLXconfig params[50];
  55.     GLXconfig* next;
  56.     GLXconfig* retconfig;
  57.     Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
  58.     XVisualInfo* vis;
  59.     XVisualInfo template;
  60.     XColor white;
  61.     XSetWindowAttributes cwa;
  62.     XWindowAttributes    pwa;
  63.     int i, nret;
  64.     Window win;
  65.  
  66.     /*
  67.      * This builds an array in "params" that describes for GLXgetconfig(3G)
  68.      * the type of GL drawing that will be done.
  69.      */
  70.     next = params;
  71.     switch (type) {
  72.       case GLXcolorIndexSingleBuffer:
  73.     set_entry(next++, GLX_NORMAL, GLX_RGB, FALSE);
  74.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, FALSE);
  75.     break;
  76.       case GLXcolorIndexDoubleBuffer:
  77.     set_entry(next++, GLX_NORMAL, GLX_RGB, FALSE);
  78.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, TRUE);
  79.     break;
  80.       case GLXrgbSingleBuffer:
  81.     set_entry(next++, GLX_NORMAL, GLX_RGB, TRUE);
  82.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, FALSE);
  83.     break;
  84.       case GLXrgbDoubleBuffer:
  85.     set_entry(next++, GLX_NORMAL, GLX_RGB, TRUE);
  86.     set_entry(next++, GLX_NORMAL, GLX_DOUBLE, TRUE);
  87.     break;
  88.     }
  89.     set_entry(next, 0, 0, 0); /* The input to GLXgetconfig is null terminated */
  90.  
  91.     /*
  92.      * Get configuration data for a window based on above parameters
  93.      * First we have to find out which screen the parent window is on,
  94.      * then we can call GXLgetconfig()
  95.      */
  96.     XGetWindowAttributes(dpy, parent, &pwa);
  97.     retconfig = GLXgetconfig(dpy, XScreenNumberOfScreen(pwa.screen), params);
  98.     if (retconfig == 0) {
  99.     printf("Sorry, can't support %s type of windows\n", typeToName[type]);
  100.     exit(-1);
  101.     }
  102.     /*
  103.      * The GL sets its own X error handlers, which aren't as informative
  104.      * when errors happen.  Calling XSetErrorHandler(0) here will
  105.      * reset back to the default Xlib version.
  106.      */
  107.     XSetErrorHandler(0);
  108.  
  109.     /*
  110.      * Scan through config info, pulling info needed to create a window
  111.      * that supports the rendering mode.
  112.      */
  113.     for (next = retconfig; next->buffer; next++) {
  114.     unsigned long buffer = next->buffer;
  115.     unsigned long mode = next->mode;
  116.     unsigned long value = next->arg;
  117.     switch (mode) {
  118.       case GLX_COLORMAP:
  119.         if (buffer == GLX_NORMAL) {
  120.         cmap = value;
  121.         }
  122.         break;
  123.       case GLX_VISUAL:
  124.         if (buffer == GLX_NORMAL) {
  125.         template.visualid = value;
  126.         template.screen = DefaultScreen(dpy);
  127.         vis = XGetVisualInfo(dpy, VisualScreenMask|VisualIDMask,
  128.                       &template, &nret);
  129.         }
  130.         break;
  131.     }
  132.     }
  133.  
  134.     /*
  135.      * Create the window
  136.      */
  137.     cwa.colormap = cmap;
  138.     cwa.border_pixel = 0;  /* Even if we don't use it, it must be something */
  139.     win = XCreateWindow(dpy, parent, x, y, w, h,
  140.                  borderWidth, vis->depth, InputOutput, vis->visual,
  141.                  CWColormap|CWBorderPixel, &cwa);
  142.  
  143.     /*
  144.      * Rescan configuration info and find window slot that getconfig
  145.      * provided.  Fill it in with the window we just created.
  146.      */
  147.     for (next = retconfig; next->buffer; next++) {
  148.     if ((next->buffer == GLX_NORMAL) && (next->mode == GLX_WINDOW)) {
  149.         next->arg = win;
  150.         break;
  151.     }
  152.     }
  153.  
  154.     /*
  155.      * Now "retconfig" contains all the information the GL needs to
  156.      * configure the window and its own internal state.
  157.      */
  158.     i = GLXlink(dpy, retconfig);
  159.     if (i < 0) {
  160.     printf("GLXlink returned %d\n", i);
  161.     exit(-1);
  162.     }
  163.  
  164.     return win;
  165. }
  166.